package com.transaction.demo.base.biz.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
import static com.transaction.demo.base.enums.ExceptionEnum.DICT_TYPE_EXIST_ERROR;
import static com.transaction.demo.base.enums.ExceptionEnum.DICT_TYPE_NOT_EXIST_ERROR;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.transaction.demo.base.biz.SysDictTypeBiz;
import com.transaction.demo.base.exception.BaseServiceException;
import com.transaction.demo.base.dao.SysDictTypeDao;
import com.transaction.demo.base.entity.SysDictTypeBean;
import com.transaction.demo.base.vo.req.SysDictTypeAddReqVo;
import com.transaction.demo.base.vo.req.SysDictTypeListReqVo;
import com.transaction.demo.base.vo.req.SysDictTypeReqVo;
import com.transaction.demo.base.vo.req.SysDictTypeUpdateReqVo;
import com.transaction.demo.base.vo.resp.SysDictTypeRespVo;
import com.transaction.demo.common.util.ResultPage;

/**
 * 描述：字典类型表
 * 作者：马庆智
 * 日期：2023/03/28 17:07:14
 */
@Slf4j
@Service
public class SysDictTypeBizImpl implements SysDictTypeBiz {

	@Autowired
	private SysDictTypeDao sysDictTypeDao;

	/**
	 * 查询字典类型表
	 *
	 * @param sysDictType 请求参数
	 * @return SysDictTypeResp
	 * @author 马庆智
	 * @date: 2023/03/28 17:07:14
	 */
	@Override
	public SysDictTypeRespVo querySysDictType(SysDictTypeReqVo sysDictType) {

		SysDictTypeBean query = sysDictTypeDao.querySysDictType(SysDictTypeBean.builder().build());

		return SysDictTypeRespVo.builder().build();
	}

	/**
	 * 查询字典类型表列表
	 *
	 * @param sysDictType 请求参数
	 * @return SysDictTypeResp
	 * @author 马庆智
	 * @date: 2023/03/28 17:07:14
	 */
	@Override
	public ResultPage<SysDictTypeRespVo> querySysDictTypeList(SysDictTypeListReqVo sysDictType) {

		PageHelper.startPage(sysDictType.getCurrent(), sysDictType.getPageSize());
		List<SysDictTypeBean> query = sysDictTypeDao.querySysDictTypeList(
				SysDictTypeBean.builder().dictTypeName(sysDictType.getDictTypeName())
						.dictTypeCode(sysDictType.getDictTypeCode()).status(sysDictType.getStatus()).build());
		PageInfo<SysDictTypeBean> pageInfo = new PageInfo<>(query);

		List<SysDictTypeRespVo> list = pageInfo.getList().stream().map(x -> {
			SysDictTypeRespVo resp = new SysDictTypeRespVo();
			BeanUtils.copyProperties(x, resp);
			return resp;
		}).collect(Collectors.toList());

		return new ResultPage<>(list, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());

	}

	/**
	 * 添加字典类型表
	 *
	 * @param addReq 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/03/28 17:07:14
	 */
	@Override
	public int saveSysDictType(SysDictTypeAddReqVo addReq) {
		SysDictTypeBean dictType = new SysDictTypeBean();
		dictType.setDictTypeCode(addReq.getDictTypeCode());
		dictType.setDictTypeName(addReq.getDictTypeName());

		SysDictTypeBean sysDictTypeBean = sysDictTypeDao.querySysDictType(dictType);
		if (sysDictTypeBean == null) {
			log.error("字典类型:{}已存在", addReq);
			throw new BaseServiceException(DICT_TYPE_EXIST_ERROR);
		}

		BeanUtils.copyProperties(addReq, dictType);
		dictType.setCreatedBy("koobe");
		dictType.setCreatedDate(new Date());
		return sysDictTypeDao.saveSysDictType(dictType);
	}

	/**
	 * 删除字典类型表
	 *
	 * @param ids 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/03/28 17:07:14
	 */
	@Override
	public int deleteSysDictType(String ids) {
		return sysDictTypeDao.deleteSysDictType(
				Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList()));
	}

	/**
	 * 更新字典类型表
	 *
	 * @param updateReq 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/03/28 17:07:14
	 */
	@Override
	public int updateSysDictType(SysDictTypeUpdateReqVo updateReq) {
		SysDictTypeBean dictType = new SysDictTypeBean();
		dictType.setDictTypeId(updateReq.getDictTypeId());

		SysDictTypeBean sysDictTypeBean = sysDictTypeDao.querySysDictType(dictType);
		if (sysDictTypeBean == null) {
			log.error("字典类型:{}不存在", updateReq);
			throw new BaseServiceException(DICT_TYPE_NOT_EXIST_ERROR);
		}

		BeanUtils.copyProperties(updateReq, dictType);
		dictType.setUpdatedBy("koobe");
		dictType.setUpdatedDate(new Date());
		return sysDictTypeDao.updateSysDictType(dictType);
	}

}